<!doctype html>
<html>
  <head>
    <!-- MathJax -->
    <script type="text/javascript"
      src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title>
      Caffe | Feature extraction with Caffe C++ code.
    </title>

    <link rel="icon" type="image/png" href="/images/caffeine-icon.png">

    <link rel="stylesheet" href="/stylesheets/reset.css">
    <link rel="stylesheet" href="/stylesheets/styles.css">
    <link rel="stylesheet" href="/stylesheets/pygment_trac.css">

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-46255508-1', 'daggerfs.com');
    ga('send', 'pageview');
  </script>
    <div class="wrapper">
      <header>
        <h1 class="header"><a href="/">Caffe</a></h1>
        <p class="header">
          Deep learning framework by <a class="header name" href="http://bair.berkeley.edu/">BAIR</a>
        </p>
        <p class="header">
          Created by
          <br>
          <a class="header name" href="http://daggerfs.com/">Yangqing Jia</a>
          <br>
          Lead Developer
          <br>
          <a class="header name" href="http://imaginarynumber.net/">Evan Shelhamer</a>
        <ul>
          <li>
            <a class="buttons github" href="https://github.com/BVLC/caffe">View On GitHub</a>
          </li>
        </ul>
      </header>
      <section>

      <h1 id="extracting-features">Extracting Features</h1>

<p>In this tutorial, we will extract features using a pre-trained model with the included C++ utility.
Note that we recommend using the Python interface for this task, as for example in the <a href="http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb">filter visualization example</a>.</p>

<p>Follow instructions for <a href="../../installation.html">installing Caffe</a> and run <code class="highlighter-rouge">scripts/download_model_binary.py models/bvlc_reference_caffenet</code> from caffe root directory.
If you need detailed information about the tools below, please consult their source code, in which additional documentation is usually provided.</p>

<h2 id="select-data-to-run-on">Select data to run on</h2>

<p>We’ll make a temporary folder to store things into.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>mkdir examples/_temp
</code></pre>
</div>

<p>Generate a list of the files to process.
We’re going to use the images that ship with caffe.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>find `pwd`/examples/images -type f -exec echo {} \; &gt; examples/_temp/temp.txt
</code></pre>
</div>

<p>The <code class="highlighter-rouge">ImageDataLayer</code> we’ll use expects labels after each filenames, so let’s add a 0 to the end of each line</p>

<div class="highlighter-rouge"><pre class="highlight"><code>sed "s/$/ 0/" examples/_temp/temp.txt &gt; examples/_temp/file_list.txt
</code></pre>
</div>

<h2 id="define-the-feature-extraction-network-architecture">Define the Feature Extraction Network Architecture</h2>

<p>In practice, subtracting the mean image from a dataset significantly improves classification accuracies.
Download the mean image of the ILSVRC dataset.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>./data/ilsvrc12/get_ilsvrc_aux.sh
</code></pre>
</div>

<p>We will use <code class="highlighter-rouge">data/ilsvrc212/imagenet_mean.binaryproto</code> in the network definition prototxt.</p>

<p>Let’s copy and modify the network definition.
We’ll be using the <code class="highlighter-rouge">ImageDataLayer</code>, which will load and resize images for us.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>cp examples/feature_extraction/imagenet_val.prototxt examples/_temp
</code></pre>
</div>

<h2 id="extract-features">Extract Features</h2>

<p>Now everything necessary is in place.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>./build/tools/extract_features.bin models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt fc7 examples/_temp/features 10 leveldb
</code></pre>
</div>

<p>The name of feature blob that you extract is <code class="highlighter-rouge">fc7</code>, which represents the highest level feature of the reference model.
We can use any other layer, as well, such as <code class="highlighter-rouge">conv5</code> or <code class="highlighter-rouge">pool3</code>.</p>

<p>The last parameter above is the number of data mini-batches.</p>

<p>The features are stored to LevelDB <code class="highlighter-rouge">examples/_temp/features</code>, ready for access by some other code.</p>

<p>If you meet with the error “Check failed: status.ok() Failed to open leveldb examples/_temp/features”, it is because the directory examples/_temp/features has been created the last time you run the command. Remove it and run again.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>rm -rf examples/_temp/features/
</code></pre>
</div>

<p>If you’d like to use the Python wrapper for extracting features, check out the <a href="http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb">filter visualization notebook</a>.</p>

<h2 id="clean-up">Clean Up</h2>

<p>Let’s remove the temporary directory now.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>rm -r examples/_temp
</code></pre>
</div>


      </section>
  </div>
  </body>
</html>
